<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library('session');	
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->model('welcome_mdl');
    } 
	
	public function index()
	{
		
		if($this->session->userdata('user_data') == false)
		{
			$this->load->view('login_view');
		}
		else
		{
			if($data["session"]["type"] == "teacher")
			{
				redirect("teacher/user");
			}
			else
			{
				redirect("home");
			}
					
		}
	}
	public function login_me()
	{
		
		$type 			= $_POST['type'];
		$username		= trim($_POST['user']);
		$password 		= md5(trim($_POST['pwd']));
		$rows = $this->db->get_where('users',array('username'=>$username , 'password' => $password , 'type' => $type))->num_rows();
		if($rows == 1)
		{			
			$row = $this->db->get_where('users',array('username'=>$username , 'password' => $password , 'type' => $type))->row();
			$this->db->where("user_id" , 	$row->user_id);
			$this->db->update('users' ,array('online' => 1));
			$this->session->set_userdata('user_data' , array(		"user_id" 	=> 	$row->user_id,
																	"uname"		=> 	$username ,
																	"type"		=>	$type,
																	"online"	=>	1
																	));
			header("Content-Type: application/json", true);
			echo json_encode(array('code' => 200,'status' => 'user can login' ));
		}
		else
		{
			header("Content-Type: application/json", true);
			echo json_encode(array('code' => 201 ,'status' => 'user can not login' ));
		}
	}
	public function logout()
	{
		$sessionData = $this->session->userdata('user_data');
		$this->db->where('user_id' , $sessionData['user_id']);
        $this->db->update('users' ,array('online' => 0));
		$this->session->userdata = array();
        $this->session->sess_destroy();
		redirect('login','refresh');
        
	}
	public function post()
	{
			$text = $_POST['text'];     
			$fp = fopen(dirname($_SERVER["SCRIPT_FILENAME"]) ."/log.html", 'a');
			fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
			fclose($fp);
	}
	public function signup()
	{
			$error = array();
			$type = $_POST["type"];
			$class = "";
			$key = 0;
			if($type == "student")
			{
				$name 			= $_POST["name"];
				if(trim($name) == "")
				{ 
					$error["error"][$key]["type"] = "name";
					$error["error"][$key]["msg"] = "User Name can not be blank.";$key++; 
				}
				
				$password 		= $_POST["password"];			
				if(trim($password) == "")
				{ 
					$error["error"][$key]["type"] = "password";
					$error["error"][$key]["msg"] = "Password can not be blank."; $key++;
				}
			
				$password_match 		= $_POST["password_match"];
				if($password != $password_match)
				{ 
					$error["error"][$key]["type"] = "password_match";
					$error["error"][$key]["msg"] = "Password and COnfirm Password are not same."; $key++;
				}
			
				$board					= $_POST["course_board"];
				if(trim($board) == "")
				{ 
					$error["error"][$key]["type"] = "course_board";
					$error["error"][$key]["msg"] = "Board can not be blank."; $key++;
				}
			
				$class					= $_POST["course_class"];
				if(trim($class) == "")
				{ 
					$error["error"][$key]["type"] = "course_class";
					$error["error"][$key]["msg"] = "Class can not be blank."; $key++;
				}
				$section_count = $this->db->get_where("users",array("type"=>"student"))->num_rows();
				if($section_count	>=50 && $section_count	<100)
				{
					$section  = "B";
				}
				elseif($section_count	< 50)
				{
					$section  = "A";
				}
				elseif($section_count	>= 100)
				{
					$section  = "C";
				}
			}
			
			if($type == "teacher")
			{
				$section		= "";
				$name 			= $_POST["name"];
				if(trim($name) == "")
				{ 
					$error["error"][$key]["type"] = "name";
					$error["error"][$key]["msg"] = "User Name can not be blank."; $key++;
				}
				
				$password 		= $_POST["password"];			
				if(trim($password) == "")
				{ 
					$error["error"][$key]["type"] = "password";
					$error["error"][$key]["msg"] = "Password can not be blank."; $key++;
				}
			
				$password_match 		= $_POST["password_match"];
				if($password != $password_match)
				{ 
					$error["error"][$key]["type"] = "password_match";
					$error["error"][$key]["msg"] = "Password and COnfirm Password are not same."; $key++;
				}
			
				$board					= $_POST["course_board"];
				if(trim($board) == "")
				{ 
					$error["error"][$key]["type"] = "course_board";
					$error["error"][$key]["msg"] = "Board can not be blank."; $key++;
				}
			}
			if($key != 0)
			{
				$error["success"] = 0;
			}
			if($key == 0)
			{
				$data 				= array();
				$data["username"]  	= trim($name);
				$data["password"]  	= md5(trim($password));
				$data["type"]  		= $type;
				$data["course"]  	= $board .'-'.$class;
				$data["section"]  	= $section;
				$data["online"]  	= 1;
				$this->db->insert("users",$data);
				$insert_id 			= $this->db->insert_id();
				$error["success"] 	= 1;
				$this->session->set_userdata('user_data' , array(	"user_id" 	=> 	$insert_id ,
																	"uname"		=> 	$name ,
																	"type"		=>	$type,
																	"online"	=>	1
																)
											);
			}
			echo json_encode($error);
			
	}
}
